package com.yoyo.itravel.dao;


import com.yoyo.itravel.bean.Permission;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/**
 * @Description 权限操作持久层接口
 *
 * @author HZX
 * @date 2020/5/24 17:45
 */
@Repository
public interface IPermissionDao {

    /**
     * @Description 添加权限
     *
     * @param permission
     * @return int
     */
    @Insert("insert into tb_permission values(#{permissionId},#{permissionName},#{url},#{parentPermissionId},#{description})")
    int insert(Permission permission) throws SQLException;

    /**
     * @Description 通过用户id查找该用户的所有权限
     *
     * @param userId
     * @return cn.hut.travel.bean.Role
     */
    @Select("SELECT p.* FROM tb_permission p WHERE permissionId IN ( SELECT r_p.permissionId FROM middle_tb_role_permission r_p WHERE roleId IN ( SELECT roleId FROM middle_tb_user_role WHERE userId=#{userId} ) )")
    List<Permission> selectByUserId(String userId) throws SQLException;

    /**
     * @Description 通过父权限查找子权限
     *
     * @param parentPermissionId
     * @return cn.hut.travel.bean.Permission
     */
    @Select("select * from tb_permission where parentPermissionId=#{parentPermissionId}")
    List<Permission> selectByParentPermissionId(String parentPermissionId) throws SQLException;

//    /**
//     * @Description 通过用户id查找该用户的所有权限
//     *
//     * @param userId
//     * @return cn.hut.travel.bean.Role
//     */
//    List<Permission> selectByUserId(String userId) throws SQLException;

}
